<!-- 自动化实验-设备运行状态-AGV小车状态详情 -->
<template>
  <!-- 顶部搜索区 -->
  <div class="main-app" v-loading="loading">
    <el-breadcrumb separator-class="el-icon-arrow-right" style="margin-top: 5px; margin-bottom: 15px;">
     <el-breadcrumb-item :to="{path:'/'}">{{$t('common.nav.home')}}</el-breadcrumb-item>
     <el-breadcrumb-item>{{$t('common.nav.automationExperiment')}}</el-breadcrumb-item>
     <el-breadcrumb-item>{{$t('common.nav.runningState')}}</el-breadcrumb-item>
     <el-breadcrumb-item>{{$t('common.nav.autoGuideVehicleDetail')}}</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 标题内容 -->
    <el-row :gutter="20">
      <el-col :span="24">
        <el-card shadow="hover" class="box-card" style="height: 80px;">
          <el-row :gutter="50">
            <el-col :span="22">
              <div class="titie-bar"><span>{{$t('common.title.autoGuideVehicle')}}</span></div>
            </el-col>
            <el-col :span="2">
              <el-button icon="el-icon-arrow-left" @click="cancel()">{{$t('common.button.back')}}</el-button>
            </el-col>
          </el-row>
        </el-card>
      </el-col>
    </el-row>
    <!-- 状态信息区域 -->
    <el-row :gutter="20">
      <!-- 状态信息 -->
      <el-col :span="18">
        <!-- 状态信息-标题 -->
        <el-card shadow="hover" class="box-card" style="height: 80px; margin-top: 10px;">
          <el-row :gutter="140">
            <el-col :span="16">
              <div class="titie-bar"><span>{{$t('common.title.statusMessage')}}</span></div>
            </el-col>
            <el-col :span="8">
              <div style="font-size: 14px; text-align: center;">
                {{$t('common.button.forceComplete')}}(<span v-if="forceCompleteStatus === true">{{$t('common.label.enable')}}</span><span v-else>{{$t('common.label.disable')}}</span>):
                <el-switch v-model="forceCompleteStatus" @change="changeForceCompleteStatus"></el-switch>
                <!-- <el-button type="primary" plain icon="el-icon-finished" @click="operation($t('common.button.forceComplete'))">{{$t('common.button.forceComplete')}}</el-button> -->
                <el-button type="primary" style="margin-left: 30px;" plain icon="el-icon-refresh" @click="operation($t('common.button.refreshStatus'))">{{$t('common.button.refreshStatus')}}</el-button>
              </div>
            </el-col>
          </el-row>
        </el-card>
        <!-- 状态信息-内容 -->
        <el-row :gutter="20">
          <el-col :span="8">
            <el-card shadow="hover" class="box-card status-panal" style="margin-top: 10px;">
              <div class="status-panal">
                <div class="status-titie"><span>{{$t('common.label.workingCondition')}}</span></div>
                <div class="status-value"><span>{{deviceStatusData.status}}</span></div>
              </div>
            </el-card>
          </el-col>
          <el-col :span="8">
            <el-card shadow="hover" class="box-card status-panal" style="margin-top: 10px;">
              <div class="status-panal">
                <div class="status-titie"><span>{{$t('common.label.currentBatteryLevel')}}</span></div>
                <div class="status-value"><span>{{deviceStatusData.batteryPower}}%</span></div>
              </div>
            </el-card>
          </el-col>
          <el-col :span="8">
            <el-card shadow="hover" class="box-card status-panal" style="margin-top: 10px;">
              <div class="status-panal">
                <div class="status-titie"><span>{{$t('common.label.positionAndOrientation')}}</span></div>
                <div class="status-value"><span>{{deviceStatusData.positionDirection}}</span></div>
              </div>
            </el-card>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="8">
            <el-card shadow="hover" class="box-card status-panal" style="margin-top: 10px;">
              <div class="status-panal">
                <div class="status-titie"><span>{{$t('common.label.runTime')}}</span></div>
                <div class="status-value"><span>{{deviceStatusData.normalRunningTime1}}h</span></div>
              </div>
            </el-card>
          </el-col>
          <el-col :span="8">
            <el-card shadow="hover" class="box-card status-panal" style="margin-top: 10px;">
              <div class="status-panal">
                <div class="status-titie"><span>{{$t('common.label.runningDistance')}}</span></div>
                <div class="status-value"><span>{{deviceStatusData.runningDistance1}}m</span></div>
              </div>
            </el-card>
          </el-col>
          <el-col :span="8">
            <el-card shadow="hover" class="box-card status-panal" style="margin-top: 10px;">
              <div class="status-panal">
                <div class="status-titie"><span>{{$t('common.label.currentLocation')}}</span></div>
                <div class="status-value"><span>X:{{deviceStatusData.xPosition}} Y:{{deviceStatusData.yPosition}}</span></div>
              </div>
            </el-card>
          </el-col>
        </el-row>
      </el-col>
      <!-- 设备照片 -->
      <el-col :span="6">
        <el-card shadow="hover" class="box-card" style="height: 450px; margin-top: 10px;">
          <div class="titie-bar"><span>{{$t('common.label.equipmentPhotos')}}</span></div>
          <img src="../../../assets/icon/assembly/composite_robot.png" style="width: 100%;" />
        </el-card>
      </el-col>
    </el-row>
    <!-- 数据列表区 -->
    <el-card class="box-card" style="margin-top: 1%;">
      <!-- 搜索条件 -->
      <el-form :inline="true" :model="searchParam" class="form-inline">
        <el-form-item :label="$t('common.label.waringContent')+':'">
          <el-input v-model="searchParam.content" clearable />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="search">{{$t('common.button.search')}}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-refresh-right" @click="reset">{{$t('common.button.reset')}}</el-button>
        </el-form-item>
      </el-form>
      <!-- 数据表格 -->
      <div class="show-result">
        <div class="table">
          <el-table id="table" ref="multipleTable" tooltip-effect="dark" stripe style="width: 100%" :data="tableData" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center"/>
            <el-table-column prop="content" :label="$t('common.label.waringContent')" align="center"/>
            <el-table-column prop="createTime" :label="$t('common.label.waringTime')" width="180" align="center">
              <template slot-scope="scope">
                {{ scope.row.createTime | parseDate }}
              </template>
            </el-table-column>
          </el-table>
        </div>
        <!-- 列表底部分页按钮 -->
        <div class="page">
          <el-pagination background layout="total, prev, pager, next, sizes"
            :total="total"
            :current-page.sync="currentPage"
            :page-sizes="[5, 10, 20, 50, 100]"
            :page-size="pageSize"
            @current-change="handleCurrentChange"
            @size-change="handleSizeChange"
          />
        </div>
      </div>
    </el-card>
  </div>
</template>

<script>
import { pageMixin } from "../../../mixins/index";
import {
  NodeDataDetail,
  SetNodeData,
  GetAutoGuideVehicleStatus
} from "../../../api/device.js";
import {
  WarningRecordList
} from "../../../api/record.js";
export default {
  // 页面公共变量
  data() {
    return {
      forceCompleteStatus: false,   // 是否启用强制完成工位
      deviceStatusData: {},         // 设备运行状态数据
      searchParam: {
        content: ""
      },                            // 搜索条件参数
    };
  },
  // 页面创建完成事件
  async created() {
    await this.init();
  },
  // 数据过滤器
  filters: {
    // 转换日期显示格式
    parseDate(value) {
      if (value) {
        return value.replace("T", " ");
      }
    },
  },
  // 表格分页属性配置
  mixins: [pageMixin],
  // 页面相关方法
  methods: {
    // 初始化加载事件
    async init() {
      this.loading = true;        // 打开加载框

      // 获取强制完成工位节点状态详情数据
      await NodeDataDetail({
        Id: 'SiemensTCP.s1500.GeneralControl.StationForceComplete1',
      }).then(res => {
        if (res.success === true) {
          // 设置数据源
          this.forceCompleteStatus = res.data.value !== null ? res.data.value : false;
        }
      });

      // 获取详情数据
      await GetAutoGuideVehicleStatus().then(res => {
        if (res.success === true) {
          // 设置数据源
          this.deviceStatusData = res.data;
          // 转换部分字段显示
          switch (this.deviceStatusData.status) {
            case "1": { this.deviceStatusData.status = this.$t('common.label.inStandby'); } break;
            case "2": { this.deviceStatusData.status = this.$t('common.label.inOperation'); } break;
            case "3": { this.deviceStatusData.status = this.$t('common.label.inFault'); } break;
            case "4": { this.deviceStatusData.status = this.$t('common.label.inOffline'); } break;
          }
        }
      });

      // 获取列表数据
      const { data, count } = await WarningRecordList({
        page: this.currentPage,
        limit: this.pageSize,
        Type: "10",
        Title: "AGV小车",
        Content: this.searchParam.content
      });
      this.tableData = data;      // 设置表格数据源
      this.total = count;         // 设置表格总数据量

      this.loading = false;       // 关闭加载框
    },
    // 表格重置事件
    async reset() {
      this.searchParam = {
        content: ""
      };
      await this.init();
    },
    // 强制完成开关选择事件
    changeForceCompleteStatus(val) {
      if (val === true) {
        // 启用强制完成工位
        this.$confirm(this.$t('common.prompt.info.isForceCompleteEnable'), this.$t('common.dialog.tip'), {
          confirmButtonText: this.$t('common.dialog.confirm'),
          cancelButtonText: this.$t('common.dialog.cancel'),
        }).then(() => {
          this.forceComplete('1');
        }).catch(() => {
          this.forceCompleteStatus = false;
        });
      } else {
        // 禁用强制完成工位
        this.$confirm(this.$t('common.prompt.info.isForceCompleteDisable'), this.$t('common.dialog.tip'), {
          confirmButtonText: this.$t('common.dialog.confirm'),
          cancelButtonText: this.$t('common.dialog.cancel'),
        }).then(() => {
          this.forceComplete('0');
        }).catch(() => {
          this.forceCompleteStatus = true;
        });
      }
    },
    // 操作功能按钮点击事件
    operation(value, row) {
      switch (value) {
        // 强制完成
        case this.$t('common.button.forceComplete'):
          this.$confirm(this.$t('common.prompt.info.isForceComplete'), this.$t('common.dialog.tip'), {
            confirmButtonText: this.$t('common.dialog.confirm'),
            cancelButtonText: this.$t('common.dialog.cancel'),
          }).then(() => {
            this.forceComplete('1');
          });
          break;
        // 刷新状态
        case this.$t('common.button.refreshStatus'):
          this.init();
          break;
      }
    },
    // 强制完成
    async forceComplete(val) {
      await SetNodeData({
        SetValueData: [{
          Id: 'SiemensTCP.s1500.GeneralControl.StationForceComplete1',
          Type: 'bool',
          Value: val
        }]
      }).then(res => {
        if (res.success === true) {
          this.$message({type: 'success', message: res.msg});
        } else {
          this.$message({type: 'error', message: res.msg});
        }
      });
    },
    // 返回事件
    cancel() {
      this.$router.back();
    },
  },
};
</script>

<style lang="scss" scoped>
// 顶部按钮绝对定位
.formbtn { text-align: right; }
// 数据表格区样式
.show-result { margin-top: 1%; }
/* 操作项开关样式 */
::v-deep .switch .el-switch__label { position: absolute; display: none; color: #fff; }
/* 打开时文字位置设置 */
::v-deep .switch .el-switch__label--right { z-index: 1; right: 5px; }
/* 关闭时文字位置设置 */
::v-deep .switch .el-switch__label--left { z-index: 1; left: 8px; }
/* 显示文字 */
::v-deep .switch .el-switch__label.is-active { display: block; }
::v-deep .switch.el-switch .el-switch__core,
::v-deep .el-switch .el-switch__label { width: 60px !important; }
/* 表格隔行换色样式 */
::v-deep .el-table--striped .el-table__body tr.el-table__row--striped td { background-color: #F2F2F2; }
/* 表格数据行高 */
::v-deep .el-table td { padding: 8px 0px; }
/* 表头背景色样式 */
::v-deep .el-table th{ background-color: #DBEDEF !important; }
/* 表头数据行内线样式 */
::v-deep .el-table td,.building-top .el-table th.is-leaf { border-bottom: 0px; }
/* 自定义提示框样式 */
.dialog_diy {
  ::v-deep .el-dialog__wrapper {
    .el-dialog {
      .el-dialog__body { padding: 0px; }
      .el-dialog__header { padding: 0px; }
      .el-dialog__headerbtn { top: 5px; right: 5px; padding-top: 10px; }
      .el-dialog__headerbtn .el-dialog__close { color: #fff; height: 30px; width: 35px; }
      .el-dialog__headerbtn .el-dialog__close:hover { color: gray; }
    }
  }
}
// 区域标题样式
.titie-bar {
  width: 4px;
  height: 32px;
  border-radius: 2px;
  background-color: rgba(61, 171, 175, 1);
  margin: 5px 0 5px 0;
}
.titie-bar span {
  color: #0A0A0A;
  width: 80px;
  font-size: 22px;
  line-height: 32px;
  white-space: nowrap;
  overflow-wrap: break-word;
  margin-left: 15px;
}

.status-panal {
  height: 175px;
  border-radius: 8px;
  //box-shadow: 0px 2px 6px 0px rgba(39, 122, 160, 0.09);
  background-size: cover;
  background-position: center center;
  background-repeat: no-repeat;
  display: flex;
  flex-direction: column;
}
.status-titie {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background-color: rgba(61, 171, 175, 1);
  margin: 5px 0 0 5px;
  display: flex;
  flex-direction: column;

  span {
    color: #0A0A0A;
    font-size: 22px;
    white-space: nowrap;
    overflow-wrap: break-word;
    margin-top: -5px;
    margin-left: 20px;
    display: flex;
    flex-direction: column;
  }
}
.status-value {
  text-align: center;
  margin-top: 12%;
  display: flex;
  flex-direction: column;

  span {
    color: rgba(1, 107, 110, 1);
    font-size: 38px;
    white-space: nowrap;
    overflow-wrap: break-word;
    display: flex;
    flex-direction: column;
  }
}
</style>
